package com.code;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;

import com.Global.Global;
import com.example.R;
import com.service.GoogleSearch;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class TK_CuocGoi extends Activity {
	// private Integer totalCall = 0, totalCallNumber = 0, totalCallSales = 0,
	// totalCallNotSales = 0;
	TextView tvTongGoiNowDate, tvTongSoDTNowDate, tvTongGiaoDichNowDate,
			tvTongKhongBanNowDate, tvTiLeGiaoDichNowDate, tvKHMoiNow,
			tvKHNgayTruoc, tvKHTrongThang, tvKHThangTruoc, tvKHTrongTuan,
			tvKhTuanTruoc;
	TextView tvTongGoiPrDate, tvTongSoDTPrDate, tvTongGiaoDichPrDate,
			tvTongKhongBanPrDate, tvTiLeGiaoDichPrDate;
	TextView tvTongGoiNMonth, tvTongSoDTNMonth, tvTongGiaoDichNMonth,
			tvTongKhongBanNMonth, tvTiLeGiaoDichNMonth;
	TextView tvTongGoiPrMonth, tvTongSoDTPrMonth, tvTongGiaoDichPrMonth,
			tvTongKhongBanPrMonth, tvTiLeGiaoDichPrMonth;

	TextView tvKHMoi, tvTongGoi, tvTongSoDT, tvTongGD, tvTongKhongGD, tvTiLeGD;

	private Integer totalCallNDate, totalCallPrDate, totalCallNMonth,
			totalCallPrMonth;

	private Integer totalCallNumberNDate, totalCallNumberPrDate,
			totalCallNumberNMonth, totalCallNumberPrMonth;

	private Integer totalCallSalesNDate, totalCallSalesPrDate,
			totalCallSalesNMonth, totalCallSalesPrMonth;

	private ArrayList<Integer> date_actual;
	private static String day;
	private static String year;
	// private static int choose;
	private static String date;
	private static int chooseType = 0;

	public static int CHOOSE_NOWDATE = 200;
	public static int CHOOSE_PREVIOUSDATE = 201;
	public static int CHOOSE_NOWMONTH = 202;
	public static int CHOOSE_PREVIOUSMONTH = 203;
	public static int CHOOSE_WEEK = 204;
	public static int CHOOSE_PREVIOUSWEEK = 205;
	public static String strNhom = "";

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		date_actual = super.getIntent().getExtras()
				.getIntegerArrayList("date_tk_cuocgoi");
		// choose =
		// super.getIntent().getExtras().getInt("choosemau_tk_cuocgoi");
		// get date 19/03/2014

		day = date_actual.get(0).toString();
		year = date_actual.get(2).toString();
		date = String.valueOf(date_actual.get(2))
				+ DetailCuocGoi.setMonth(date_actual.get(1))
				+ Global.formatDay(date_actual.get(0));
		chooseType = super.getIntent().getExtras().getInt("chooseType");
		strNhom = super.getIntent().getExtras().getString("strKho");

		if (chooseType == TK_ChonMau.CHOOSE_TKCUOCGOI) {
			setContentView(R.layout.tk_cuocgoi);
			// now
			tvTongGoiNowDate = (TextView) findViewById(R.id.tvTongGoiNowDate);
			tvTongSoDTNowDate = (TextView) findViewById(R.id.tvTongSoDT);
			tvTongGiaoDichNowDate = (TextView) findViewById(R.id.tvTongGiaoDich);
			tvTongKhongBanNowDate = (TextView) findViewById(R.id.tvTongKhongBan);
			tvTiLeGiaoDichNowDate = (TextView) findViewById(R.id.tvTiLeGiaoDich);
			// text view khach hang
			tvKHMoiNow = (TextView) findViewById(R.id.tvKHMoiNow);

			// set text total call
			totalCallNDate = countTotalCall(CHOOSE_NOWDATE);
			tvTongGoiNowDate.setText(totalCallNDate.toString());
			totalCallNumberNDate = countTotalCallNumberPhone(CHOOSE_NOWDATE);
			totalCallSalesNDate = countTotalCallSales(CHOOSE_NOWDATE);
			tvTongSoDTNowDate.setText(totalCallNumberNDate.toString());
			tvTongGiaoDichNowDate.setText(totalCallSalesNDate.toString());
			tvTongKhongBanNowDate.setText(String
					.valueOf(countTotalCallNotSalses(CHOOSE_NOWDATE)));
			tvKHMoiNow.setText(String.valueOf(countSupplier(CHOOSE_NOWDATE)));

			// now
			if (totalCallNDate != 0) {
				Float fCallNumber = Float.parseFloat(totalCallSalesNDate
						.toString());
				Float fCallSales = Float.parseFloat(totalCallNDate.toString());
				DecimalFormat df = new DecimalFormat("##.##");

				tvTiLeGiaoDichNowDate.setText(String.valueOf(df
						.format((fCallNumber / fCallSales) * 100)) + " %");
			} else
				tvTiLeGiaoDichNowDate.setText("0" + " %");

			// previous date
			tvTongGoiPrDate = (TextView) findViewById(R.id.tvTongGoiNgayTruoc);
			tvTongSoDTPrDate = (TextView) findViewById(R.id.tvTongSoDTNgayTruoc);
			tvTongGiaoDichPrDate = (TextView) findViewById(R.id.tvTongGiaoDichNgayTruoc);
			tvTongKhongBanPrDate = (TextView) findViewById(R.id.tvTongKhongBanNgayTruoc);
			tvTiLeGiaoDichPrDate = (TextView) findViewById(R.id.tvTiLeGiaoDichNgayTruoc);

			// now month
			tvTongGoiNMonth = (TextView) findViewById(R.id.tvTongGoiTrongThang);
			tvTongSoDTNMonth = (TextView) findViewById(R.id.tvTongSoDTTrongThang);
			tvTongGiaoDichNMonth = (TextView) findViewById(R.id.tvTongGiaoDichTrongThang);
			tvTongKhongBanNMonth = (TextView) findViewById(R.id.tvTongKhongBanTrongThang);
			tvTiLeGiaoDichNMonth = (TextView) findViewById(R.id.tvTiLeGiaoDichTrongThang);

			// previous month
			tvTongGoiPrMonth = (TextView) findViewById(R.id.tvTongGoiThangTruoc);
			tvTongSoDTPrMonth = (TextView) findViewById(R.id.tvTongSoDTThangTruoc);
			tvTongGiaoDichPrMonth = (TextView) findViewById(R.id.tvTongGiaoDichThangTruoc);
			tvTongKhongBanPrMonth = (TextView) findViewById(R.id.tvTongKhongBanThangTruoc);
			tvTiLeGiaoDichPrMonth = (TextView) findViewById(R.id.tvTiLeGiaoDichThangTruoc);

			tvKHNgayTruoc = (TextView) findViewById(R.id.tvKHNgayTruoc);
			tvKHTrongThang = (TextView) findViewById(R.id.tvKHTrongThang);
			tvKHThangTruoc = (TextView) findViewById(R.id.tvKHThangTruoc);
			tvKHTrongTuan = (TextView) findViewById(R.id.tvKHTrongTuan);
			tvKhTuanTruoc = (TextView) findViewById(R.id.tvKHTuanTruoc);

			totalCallPrDate = countTotalCall(CHOOSE_PREVIOUSDATE);
			totalCallNMonth = countTotalCall(CHOOSE_NOWMONTH);
			totalCallPrMonth = countTotalCall(CHOOSE_PREVIOUSMONTH);

			tvTongGoiPrDate.setText(totalCallPrDate.toString());
			tvTongGoiNMonth.setText(totalCallNMonth.toString());
			tvTongGoiPrMonth.setText(totalCallPrMonth.toString());
			// end total call

			// total call number phone

			totalCallNumberPrDate = countTotalCallNumberPhone(CHOOSE_PREVIOUSDATE);
			totalCallNumberNMonth = countTotalCallNumberPhone(CHOOSE_NOWMONTH);
			totalCallNumberPrMonth = countTotalCallNumberPhone(CHOOSE_PREVIOUSMONTH);

			// total call sales

			totalCallSalesPrDate = countTotalCallSales(CHOOSE_PREVIOUSDATE);
			totalCallSalesNMonth = countTotalCallSales(CHOOSE_NOWMONTH);
			totalCallSalesPrMonth = countTotalCallSales(CHOOSE_PREVIOUSMONTH);

			// set text total call number

			tvTongSoDTPrDate.setText(totalCallNumberPrDate.toString());
			tvTongSoDTNMonth.setText(totalCallNumberNMonth.toString());
			tvTongSoDTPrMonth.setText(totalCallNumberPrMonth.toString());
			// end total call sales

			// set text total call sales

			tvTongGiaoDichPrDate.setText(totalCallSalesPrDate.toString());
			tvTongGiaoDichNMonth.setText(totalCallSalesNMonth.toString());
			tvTongGiaoDichPrMonth.setText(totalCallSalesPrMonth.toString());
			// end total call sales

			// set text total call not sales

			tvTongKhongBanPrDate.setText(String
					.valueOf(countTotalCallNotSalses(CHOOSE_PREVIOUSDATE)));
			tvTongKhongBanNMonth.setText(String
					.valueOf(countTotalCallNotSalses(CHOOSE_NOWMONTH)));
			tvTongKhongBanPrMonth.setText(String
					.valueOf(countTotalCallNotSalses(CHOOSE_PREVIOUSMONTH)));
			// end total call not sales

			// set text tk khach hang

			tvKHNgayTruoc.setText(String
					.valueOf(countSupplier(CHOOSE_PREVIOUSDATE)));
			tvKHTrongThang.setText(String
					.valueOf(countSupplier(CHOOSE_NOWMONTH)));
			tvKHThangTruoc.setText(String
					.valueOf(countSupplier(CHOOSE_PREVIOUSMONTH)));
			tvKHTrongTuan.setText(String.valueOf(countSupplier(CHOOSE_WEEK)));
			tvKhTuanTruoc.setText(String
					.valueOf(countSupplier(CHOOSE_PREVIOUSWEEK)));
			// end kh

			// pr date
			if (totalCallPrDate != 0) {
				Float fCallNumber = Float.parseFloat(totalCallSalesPrDate
						.toString());
				Float fCallSales = Float.parseFloat(totalCallPrDate.toString());
				DecimalFormat df = new DecimalFormat("##.##");

				tvTiLeGiaoDichPrDate.setText(String.valueOf(df
						.format((fCallNumber / fCallSales) * 100)) + " %");
			} else
				tvTiLeGiaoDichPrDate.setText("0" + " %");
			// now month
			if (totalCallNMonth != 0) {
				Float fCallNumber = Float.parseFloat(totalCallSalesNMonth
						.toString());
				Float fCallSales = Float.parseFloat(totalCallNMonth.toString());
				DecimalFormat df = new DecimalFormat("##.##");

				tvTiLeGiaoDichNMonth.setText(String.valueOf(df
						.format((fCallNumber / fCallSales) * 100)) + " %");
			} else
				tvTiLeGiaoDichNMonth.setText("0" + " %");

			// pr month
			if (totalCallPrMonth != 0) {
				Float fCallNumber = Float.parseFloat(totalCallSalesPrMonth
						.toString());
				Float fCallSales = Float
						.parseFloat(totalCallPrMonth.toString());
				DecimalFormat df = new DecimalFormat("##.##");

				tvTiLeGiaoDichPrMonth.setText(String.valueOf(df
						.format((fCallNumber / fCallSales) * 100)) + " %");
			} else
				tvTiLeGiaoDichPrMonth.setText("0" + " %");
		} else {
			setContentView(R.layout.tk_cuocgoitheokho);

			// now
			tvTongGoi = (TextView) findViewById(R.id.tvTongGoi);
			tvTongSoDT = (TextView) findViewById(R.id.tvTongSoDT);
			tvTongGD = (TextView) findViewById(R.id.tvTongGD);
			tvTongKhongGD = (TextView) findViewById(R.id.tvTongKhongGD);
			tvTiLeGD = (TextView) findViewById(R.id.tvTiLeGD);
			// text view khach hang
			tvKHMoi = (TextView) findViewById(R.id.tvKHMoi);

			// set text total call
			totalCallNDate = countTotalCall(CHOOSE_NOWDATE);
			tvTongGoi.setText(totalCallNDate.toString());
			totalCallNumberNDate = countTotalCallNumberPhone(CHOOSE_NOWDATE);
			totalCallSalesNDate = countTotalCallSales(CHOOSE_NOWDATE);
			tvTongSoDT.setText(totalCallNumberNDate.toString());
			tvTongGD.setText(totalCallSalesNDate.toString());
			tvTongKhongGD.setText(String
					.valueOf(countTotalCallNotSalses(CHOOSE_NOWDATE)));
			tvKHMoi.setText(String.valueOf(countSupplier(CHOOSE_NOWDATE)));

			// now
			if (totalCallNDate != 0) {
				Float fCallNumber = Float.parseFloat(totalCallSalesNDate
						.toString());
				Float fCallSales = Float.parseFloat(totalCallNDate.toString());
				DecimalFormat df = new DecimalFormat("##.##");

				tvTiLeGD.setText(String.valueOf(df
						.format((fCallNumber / fCallSales) * 100)) + " %");
			} else
				tvTiLeGD.setText("0" + " %");
		}
	}

	/**
	 * count total Call
	 * 
	 * @author trangbm
	 * @return date 25/03/2014
	 */
	public int countTotalCall(int type) {
		int totalCall = 0;
		StringBuffer sql = new StringBuffer(
				" SELECT count(ID) as total FROM Cuocgoi WHERE ");
		if (type == CHOOSE_NOWDATE)
			sql.append(" cast(Ngay as date) = '" + date + "'");
		else if (type == CHOOSE_PREVIOUSDATE)
			sql.append(" cast(Ngay as date) = DATEADD(DD,-1,'" + date + "')");
		else if (type == CHOOSE_NOWMONTH) {
			sql.append(" DATEPART(MM,Ngay) = " + date_actual.get(1));
			sql.append(" AND DATEPART(YYYY,Ngay) = " + year);
		} else if (type == CHOOSE_PREVIOUSMONTH) {
			sql.append(" DATEPART(MM,Ngay) = " + (date_actual.get(1) - 1));
			sql.append(" AND DATEPART(YYYY,Ngay) = " + year);
		}
		if (strNhom.equals(""))
			sql.append(" AND Kho COLLATE DATABASE_DEFAULT in (select value from  dbo.fn_Split('"
					+ strNhom + "', ','))");
		try {
			ResultSet rs = Global.excuteQuerySQLServer(sql.toString());
			if (rs.next())
				totalCall = rs.getInt("total");
			rs.close();
			// conn.close();
			return totalCall;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return totalCall;
	}

	/**
	 * @author Administrator
	 * @return date 25/03/2014
	 */
	public int countTotalCallNumberPhone(int type) {
		int totalCall = 0;
		StringBuffer sql = new StringBuffer(
				" SELECT count(distinct Phone) as total FROM Cuocgoi WHERE ");
		if (type == CHOOSE_NOWDATE)
			sql.append(" cast(Ngay as date) = '" + date + "'");
		else if (type == CHOOSE_PREVIOUSDATE)
			sql.append(" cast(Ngay as date) = DATEADD(DD,-1,'" + date + "')");
		else if (type == CHOOSE_NOWMONTH) {
			sql.append(" DATEPART(MM,Ngay) = " + date_actual.get(1));
			sql.append(" AND DATEPART(YYYY,Ngay) = " + year);
		} else if (type == CHOOSE_PREVIOUSMONTH) {
			sql.append(" DATEPART(MM,Ngay) = " + (date_actual.get(1) - 1));
			sql.append(" AND DATEPART(YYYY,Ngay) = " + year);
		}
		if (strNhom.equals(""))
			sql.append(" AND MSK COLLATE DATABASE_DEFAULT in (select value from  dbo.fn_Split('"
					+ strNhom + "', ','))");
		try {
			ResultSet rs = Global.excuteQuerySQLServer(sql.toString());
			if (rs.next())
				totalCall = rs.getInt("total");
			rs.close();
			// conn.close();
			return totalCall;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return totalCall;
	}

	/**
	 * count total call
	 * 
	 * @author trangbm
	 * @return date 25/03/2014
	 */
	public int countTotalCallSales(int type) {
		int totalCall = 0;
		StringBuffer sql = new StringBuffer(
				" SELECT count(ID) as total FROM Cuocgoi WHERE ISNULL(SoPhieu,'') !='' AND ");
		if (type == CHOOSE_NOWDATE)
			sql.append(" cast(Ngay as date) = '" + date + "'");
		else if (type == CHOOSE_PREVIOUSDATE)
			sql.append(" cast(Ngay as date) = DATEADD(DD,-1,'" + date + "')");
		else if (type == CHOOSE_NOWMONTH) {
			sql.append(" DATEPART(MM,Ngay) = " + date_actual.get(1));
			sql.append(" AND DATEPART(YYYY,Ngay) = " + year);
		} else if (type == CHOOSE_PREVIOUSMONTH) {
			sql.append(" DATEPART(MM,Ngay) = " + (date_actual.get(1) - 1));
			sql.append(" AND DATEPART(YYYY,Ngay) = " + year);
		}
		if (strNhom.equals(""))
			sql.append(" AND MSK COLLATE DATABASE_DEFAULT in (select value from  dbo.fn_Split('"
					+ strNhom + "', ','))");
		try {
			ResultSet rs = Global.excuteQuerySQLServer(sql.toString());
			if (rs.next())
				totalCall = rs.getInt("total");
			rs.close();
			// conn.close();
			return totalCall;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return totalCall;
	}

	/**
	 * count total call not Sales
	 * 
	 * @author trangbm
	 * @return date 25/03/2014
	 */
	public int countTotalCallNotSalses(int type) {
		int totalCall = 0;
		StringBuffer sql = new StringBuffer(
				" SELECT count(ID) as total FROM Cuocgoi WHERE ISNULL(SoPhieu,'') ='' AND");
		if (type == CHOOSE_NOWDATE)
			sql.append(" cast(Ngay as date) = '" + date + "'");
		else if (type == CHOOSE_PREVIOUSDATE)
			sql.append(" cast(Ngay as date) = DATEADD(DD,-1,'" + date + "')");
		else if (type == CHOOSE_NOWMONTH) {
			sql.append(" DATEPART(MM,Ngay) = " + date_actual.get(1));
			sql.append(" AND DATEPART(YYYY,Ngay) = " + year);
		} else if (type == CHOOSE_PREVIOUSMONTH) {
			sql.append(" DATEPART(MM,Ngay) = " + (date_actual.get(1) - 1));
			sql.append(" AND DATEPART(YYYY,Ngay) = " + year);
		}
		if (strNhom.equals(""))
			sql.append(" AND MSK COLLATE DATABASE_DEFAULT in (select value from  dbo.fn_Split('"
					+ strNhom + "', ','))");
		try {
			ResultSet rs = Global.excuteQuerySQLServer(sql.toString());
			if (rs.next())
				totalCall = rs.getInt("total");
			rs.close();
			// conn.close();
			return totalCall;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return totalCall;
	}

	public int countSupplier(int type) {
		int count = 0;
		StringBuffer sql = new StringBuffer(
				" SELECT count(distinct([Supplier ID])) ");
		sql.append(" FROM Suppliers WHERE ");
		if (type == CHOOSE_NOWDATE)
			sql.append(" cast(Ngaygio as date) = '" + date + "'");
		else if (type == CHOOSE_PREVIOUSDATE)
			sql.append(" cast(Ngaygio as date) = DATEADD(DD,-1,'" + date + "')");
		else if (type == CHOOSE_NOWMONTH) {
			sql.append(" DATEPART(MM,Ngaygio) = " + date_actual.get(1));
			sql.append(" AND DATEPART(YYYY,Ngaygio) = " + year);
		} else if (type == CHOOSE_PREVIOUSMONTH) {
			sql.append(" DATEPART(MM,Ngaygio) = " + (date_actual.get(1) - 1));
			sql.append(" AND DATEPART(YYYY,Ngaygio) = " + year);
		} else if (type == CHOOSE_WEEK) {
			sql.append(" DATEPART(WEEK,Ngaygio) = DATEPART(WEEK,'" + date
					+ "')");
			sql.append(" AND DATEPART(YYYY,Ngaygio) = " + year);
		} else if (type == CHOOSE_PREVIOUSWEEK) {
			sql.append(" DATEPART(WEEK,Ngaygio) = DATEPART(WEEK,'" + date
					+ "') -1 ");
			sql.append(" AND DATEPART(YYYY,Ngaygio) = " + year);
		}
		// chi nhanh by Kho
		if (!strNhom.equals("")) {
			sql.append(" AND ChiNhanh IN (select ChiNhanhID From kho where MSK COLLATE DATABASE_DEFAULT in "
					+ " (SELECT value FROM  dbo.fn_Split('"
					+ strNhom
					+ "', ',')))");
		}
		try {
			ResultSet rs = Global.excuteQuerySQLServer(sql.toString());
			if (rs.next())
				count = rs.getInt(1);
			rs.close();
			// conn.close();
			return count;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
}
